(function() {
    let pageNumber = 0;
    getDate(pageNumber);
    //遍历页码,并渲染
    let option = '';
    for (let index = 0; index < 14; index++) {
        option += `
        <option value="${index}">${index+1}</option>
        `
    };
    $('#number').html(option);
    //事件函数
    $('#prev').on('click', function() {
        pageNumber--;
        $('#number')[0].value = pageNumber;
        isRequest(pageNumber);
    });
    $('#next').on('click', function() {
        pageNumber++;
        $('#number')[0].value = pageNumber;
        isRequest(pageNumber)
    });
    $('#number').on('change', function() {
        pageNumber = this.value;
        isRequest(pageNumber)
    });
    //判断是否调用请求
    function isRequest(num) {
        if (num < 0) {
            $('#prev').attr('disabled', true);
            $('#number')[0].value = 0;
        } else if (num > 13) {
            $('#next').attr('disabled', true);
            $('#number')[0].value = 13;
        } else {
            $('#prev').attr('disabled', false)
            $('#next').attr('disabled', false)
            getDate(num);
        }

    }
    //下拉框监听事件

    function getDate(index = 0) {
        index -= 0;
        fetch('http://chst.vip:1234/api/getmoneyctrl?pageid=' + index).then(body => body.json()).then(res => {
            let result = res.result;
            console.log(result);
            let discStr = '';
            result.forEach(item => {
                discStr += `
                <li date-productId="${item.productId}">
                <a href="javascript:;">
                    <div class="imgico">
                        ${item.productImg2}
                    </div>
                    <div class="info">
                        <h3>${item.productName}</h3>
                        <span class="price">¥ ${item.productPinkage}</span>
                        <div class="desc">
                            <span class="times">${item.productFrom} | ${item.productTime}</span>
                            <div class="review">
                                <i></i>
                                <span>${item.productComCount}</span>
                            </div>
                        </div>
                    </div>
                </a>
            </li>
                `
            });
            $('.discBody >ul').html(discStr);
        })
    };
    $('.discBody').on('click', 'li', function() {

        location.href = '../html/savemoneyXQ.html?productid=' + $(this).attr('date-productId')
    })

})()